package com.deve.pig.controller.dic;

import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import my.utils.Constants;
import my.utils.DwzAjaxJsonUtil;
import my.utils.Pagination;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.deve.pig.controller.BaseController;
import com.deve.pig.model.Category;
import com.deve.pig.model.CategoryExample;
import com.deve.pig.model.CategoryExample.Criteria;
import com.deve.pig.service.category.ICategoryService;

/**
 * @author tanyouzhong
 * @description 文献分类控制器
 * @date 2014年7月12日 下午11:13:01
 */
@Controller
@RequestMapping("/dic/category")
public class CatagoryController extends BaseController
{
	@Resource
	private ICategoryService categoryService;

	@Resource(name = "categoryStatusStr")
	private LinkedHashMap<String, String> categoryStatus;

	/**
	 * @Description:显示列表页
	 */
	@RequestMapping("/showList")
	public String showIndex(Model model, HttpServletRequest request)
	{
		Map<String, Object> params = super.getRequestParameters(request);
		int pageNum = Pagination.CURRENTPAGE;
		int pageSize = Pagination.PAGESIZE;
		if (params.containsKey("pageNum"))
		{
			pageNum = Integer.parseInt((String) params.get("pageNum"));
		}
		if (params.containsKey("numPerPage"))
		{
			pageSize = Integer.parseInt((String) params.get("numPerPage"));
		}
		params.put("pid", 0);
		Pagination page = categoryService.findPagination(params, pageNum, pageSize);

		model.addAttribute("page", page);

		return "admin/dic/category/category";
	}

	@RequestMapping("/showAdd")
	public String showAdd(Model model)
	{
		model.addAttribute("categoryStatus", categoryStatus);
		return "admin/dic/category/add";
	}

	@RequestMapping("/add")
	@ResponseBody
	public Map<String, Object> add(Category category,
			HttpServletRequest request) throws IOException
	{
		Map<String, Object> json = DwzAjaxJsonUtil.getDefaultAjaxJson();
		json.put(DwzAjaxJsonUtil.KEY_NAVTABID, getNavTabId(request));
		json.put(DwzAjaxJsonUtil.KEY_DIALOGID, getDialogId(request));
		if (category == null)
		{
			json.put(DwzAjaxJsonUtil.KEY_STATUSCODE, 300);
			json.put(DwzAjaxJsonUtil.KEY_MESSAGE, "操作失败！");
		}
		else
		{
			if(category.getPid() == null)
				category.setPid(0);
			if (categoryService.add(category) < 1)
			{
				json.put(DwzAjaxJsonUtil.KEY_STATUSCODE, 300);
				json.put(DwzAjaxJsonUtil.KEY_MESSAGE, "抱歉，服务器故障，请重试");
			}
			json.put("pid", category.getPid());
		}
		return json;
	}

	@RequestMapping("/delete/{id}")
	@ResponseBody
	public Map<String, Object> delete(@PathVariable("id") int id, HttpServletRequest request) throws IOException
	{
		Map<String, Object> json = DwzAjaxJsonUtil.getDefaultAjaxJson();
		json.put(DwzAjaxJsonUtil.KEY_NAVTABID, getNavTabId(request));
		json.put(DwzAjaxJsonUtil.KEY_DIALOGID, getDialogId(request));
		json.put(DwzAjaxJsonUtil.KEY_CALLBACKTYPE, "");
		byte categoryStatus = categoryService.findById(id).getStatus();
		if(Constants.CategoryStatus.SHIELD == categoryStatus)
		{
			if(categoryService.findCountByPid(id, Arrays.asList(Constants.CategoryStatus.NORMAL, Constants.CategoryStatus.SHIELD)) < 1)
			{
				if (categoryService.delete(id) < 1)
				{
					json.put(DwzAjaxJsonUtil.KEY_STATUSCODE, 300);
					json.put(DwzAjaxJsonUtil.KEY_MESSAGE, "删除失败！");
				}
			}
			else
			{
				json.put(DwzAjaxJsonUtil.KEY_STATUSCODE, 300);
				json.put(DwzAjaxJsonUtil.KEY_MESSAGE, "该分类下存在子分类，禁止删除");
			}
		}
		else
		{
			json.put(DwzAjaxJsonUtil.KEY_STATUSCODE, 300);
			json.put(DwzAjaxJsonUtil.KEY_MESSAGE, "该分类已启用，禁止删除");
		}
		return json;
	}

	@RequestMapping("/showUpdate/{id}")
	public String showUpdate(@PathVariable("id") int id, Model model)
	{
		Category category = categoryService.findById(id);
		model.addAttribute("category", category);
		model.addAttribute("categoryStatus", categoryStatus);
		return "admin/dic/category/update";
	}

	@RequestMapping("/update")
	@ResponseBody
	public Map<String, Object> update(Category category,
			HttpServletRequest request) throws IOException
	{
		Map<String, Object> json = DwzAjaxJsonUtil.getDefaultAjaxJson();
		json.put(DwzAjaxJsonUtil.KEY_NAVTABID, getNavTabId(request));
		json.put(DwzAjaxJsonUtil.KEY_DIALOGID, getDialogId(request));
		if (category == null)
		{
			json.put(DwzAjaxJsonUtil.KEY_STATUSCODE, 300);
			json.put(DwzAjaxJsonUtil.KEY_MESSAGE, "操作失败！");
		}
		else
		{
			if (categoryService.update(category) < 1)
			{
				json.put(DwzAjaxJsonUtil.KEY_STATUSCODE, 300);
				json.put(DwzAjaxJsonUtil.KEY_MESSAGE, "抱歉，服务器故障，请重试");
			}
		}
		return json;
	}

	/**
	 * @Description: 置顶分类
	 */
	@RequestMapping("/activation")
	@ResponseBody
	public Map<String, Object> activation(Category category, HttpServletRequest request) throws IOException
	{
		Map<String, Object> json = DwzAjaxJsonUtil.getDefaultAjaxJson();
		json.put(DwzAjaxJsonUtil.KEY_NAVTABID, getNavTabId(request));
		json.put(DwzAjaxJsonUtil.KEY_DIALOGID, getDialogId(request));
		json.put(DwzAjaxJsonUtil.KEY_CALLBACKTYPE, "");
		if (category == null)
		{
			json.put(DwzAjaxJsonUtil.KEY_STATUSCODE, 300);
			json.put(DwzAjaxJsonUtil.KEY_MESSAGE, "操作失败！");
		}
		else
		{
			category.setStatus(Constants.CategoryStatus.NORMAL);
			if (categoryService.update(category) < 1)
			{
				json.put(DwzAjaxJsonUtil.KEY_STATUSCODE, 300);
				json.put(DwzAjaxJsonUtil.KEY_MESSAGE, "抱歉，服务器故障，请重试");
			}
		}
		return json;
	}
	
	/**
	 * @Description: 屏蔽分类
	 */
	@RequestMapping("/shield")
	@ResponseBody
	public Map<String, Object> shieldAudio(Category category, HttpServletRequest request) throws IOException
	{
		Map<String, Object> json = DwzAjaxJsonUtil.getDefaultAjaxJson();
		json.put(DwzAjaxJsonUtil.KEY_NAVTABID, getNavTabId(request));
		json.put(DwzAjaxJsonUtil.KEY_DIALOGID, getDialogId(request));
		json.put(DwzAjaxJsonUtil.KEY_CALLBACKTYPE, "");
		if ( category == null)
		{
			json.put(DwzAjaxJsonUtil.KEY_STATUSCODE, 300);
			json.put(DwzAjaxJsonUtil.KEY_MESSAGE, "操作失败！");
		}
		else
		{
			category.setStatus(Constants.CategoryStatus.SHIELD);
			if (categoryService.update(category) < 1)
			{
				json.put(DwzAjaxJsonUtil.KEY_STATUSCODE, 300);
				json.put(DwzAjaxJsonUtil.KEY_MESSAGE, "抱歉，服务器故障，请重试");
			}
		}
		return json;
	}

	@RequestMapping("/showSetSubCategory")
	public String showSetSubPriv(Category category, Model model, HttpServletRequest request)
	{
		CategoryExample example = new CategoryExample();
		Criteria criteria = example.createCriteria();
		criteria.andPidEqualTo(category.getPid());
		model.addAttribute("list", categoryService.findByCriteria(example));

		return "admin/dic/category/showSetSubCategory";

	}
}